package xyz.zhzby.cyzx.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import xyz.zhzby.cyzx.cms.convert.ChannelConvert;
import xyz.zhzby.cyzx.cms.dto.response.ChannelResponse;
import xyz.zhzby.cyzx.cms.dto.response.Info;
import xyz.zhzby.cyzx.cms.entity.Channel;
import xyz.zhzby.cyzx.cms.mapper.ChannelMapper;
import xyz.zhzby.cyzx.cms.service.IArticleChannelService;
import xyz.zhzby.cyzx.cms.service.IChannelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.zhzby.cyzx.security.entity.User;
import xyz.zhzby.cyzx.security.util.CyzxSecurityContextHolder;
import xyz.zhzby.cyzx.servicebase.domain.DataLogInfo;
import xyz.zhzby.cyzx.servicebase.domain.RestResponse;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 文章分类分栏（个人设置） 服务实现类
 * </p>
 *
 * @author zby
 * @since 2021-12-10
 */
@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, Channel> implements IChannelService {

    @Autowired
    private IArticleChannelService articleChannelService;

    @Override
    public void createChannel(Channel channel) {
        User subject = CyzxSecurityContextHolder.getSubject();
        channel.setUserId(subject.getId());
        subject.fillCreateInfo(channel, DataLogInfo.USER_FIELD_ACTUAL_NAME);
        baseMapper.insert(channel);
    }

    @Override
    public RestResponse<String> updateChannel(Channel channel) {
        User subject = CyzxSecurityContextHolder.getSubject();
        subject.fillUpdateInfo(channel, DataLogInfo.USER_FIELD_ACTUAL_NAME);
        baseMapper.updateById(channel);
        return RestResponse.success("修改成功");
    }

    @Override
    public ChannelResponse getChannelById(Long channelID) {
        Channel channel = baseMapper.selectById(channelID);
        ChannelResponse response = ChannelConvert.INSTANCE.CHANNEL_RESPONSE(channel);
        response.setCount(articleChannelService.getArticleCountByChannelId(channel.getId()));
        return response;
    }

    @Override
    public RestResponse<List<ChannelResponse>> getAllChannels() {
        User subject = CyzxSecurityContextHolder.getSubject();
        LambdaQueryWrapper<Channel> qw = new LambdaQueryWrapper<>();
        qw.eq(Channel::getUserId, subject.getId());
        qw.orderByAsc(Channel::getId);
        List<Channel> channels = baseMapper.selectList(qw);
        List<ChannelResponse> channelResponses = ChannelConvert.INSTANCE.CHANNEL_RESPONSE_LIST(channels);
        return RestResponse.success(channelResponses);
    }

    @Override
    public RestResponse<String> deleteChannel(Long channelId) {
        articleChannelService.deleteLinkByChannelId(channelId);
        baseMapper.deleteById(channelId);
        return RestResponse.success("删除成功");
    }

    @Override
    public List<Info> getLinkChannelsByArticleID(Long articleID) {
        return baseMapper.getLinkChannelsByArticleID(articleID);
    }
}
